package cn.e4j.modular.api.controller;

import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ErrorResponseData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.reqres.response.SuccessResponseData;
import cn.e4j.core.util.DateUtils;
import cn.e4j.modular.api.service.FwpjService;
import cn.e4j.modular.api.service.GwbService;
import cn.e4j.modular.api.service.MmrjService;
import cn.e4j.modular.api.util.ControllerSupport;
import cn.e4j.modular.system.entity.SysUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * Created by yuan_rz on 2018/04/12.
 */
@RestController
@RequestMapping("api/wx/fwpj")
public class FwpjController extends ControllerSupport {
    @Autowired
    private FwpjService fwpjService;

    @Autowired
    private MmrjService mmrjService;

    @Autowired
    private GwbService gwbService;

    /**
     * 获取服务单列表
     *
     * @return
     */
    @RequestMapping(value = "getServiceOrderListData")
    public ResponseData getServiceOrderListData(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        List<Dto> list = new ArrayList<Dto>();
        SysUser user = wapper.getSysUser();
        try {
            inDto.put("MMRDDOCDATE", DateUtils.parseDate(inDto.getString("MMRDDOCDATE")));
            inDto.put("MMRDENT", inDto.getString("ENT"));

            if (inDto.getString("GWAA007").equals("0")) {
                //内部员工
                inDto.put("USERID", user.getID());

                //客户权限
                inDto.put("GWAK006", "1");
                Dto gwb001 = gwbService.getGwb001(user, inDto);
                List<Dto> _list = (List<Dto>) gwb001.get("CUSTOMERS");
                inDto.put("CUSTOMERS", _list);
            } else if (inDto.getString("GWAA007").equals("1")) {
                List<Dto> clist = new ArrayList<Dto>();
                Dto dto = new Dto();
                dto.put("ID", inDto.getString("GWAK030"));
                clist.add(dto);
                inDto.put("CUSTOMERS", clist);
            }

            list = fwpjService.getServiceOrderListData(wapper.getPage(), inDto);
        } catch (Exception e) {
            e.printStackTrace();
            return ERROR;
        }
        return SUCCESS(list);

    }

    /**
     * 获取编号和星数
     *
     * @return
     */
    @RequestMapping(value = "getStarData")
    public ResponseData getStarData(@DtoParam DtoWapper wapper) {
        List<Dto> list = new ArrayList<Dto>();
        Dto inDto = wapper.getDto();
        try {
            //标准评价表 mmrj_t
            Dto mmrjDto = new Dto();
            mmrjDto.put("MMRJENT", inDto.getString("ENT"));
            mmrjDto.put("MMRJ002", "1");
            list = mmrjService.getMmrjOne(wapper.getPage(), mmrjDto);
        } catch (Exception e) {
            e.printStackTrace();
            return ERROR;
        }
        return SUCCESS(list);
    }

    /**
     * 服务评价提交
     *
     * @return
     */
    @RequestMapping(value = "saveFwpj")
    public ResponseData saveFwpj(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        try {
            fwpjService.saveFwpj(wapper.getSysUser(), inDto);
        } catch (Exception e) {
            e.printStackTrace();
            return ERROR;
        }
        return SUCCESS;
    }

    /**
     * 获取客户对服务的评价
     */
    @RequestMapping(value = "getFwpjList")
    public ResponseData getFwpjList(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        List<Dto> list = new ArrayList<>();
        try {
            inDto.put("MMRCENT", inDto.getString("ENT"));
            list = fwpjService.getFwpjOfCustomer(inDto);
        } catch (Exception e) {
            e.printStackTrace();
            return ERROR;
        }
        return SUCCESS(list);
    }

}
